﻿Imports System.Data.SqlClient
Public Class Frm_khibra
    Public dv As Dossier
    Public avo As Khibra

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            Me.Enabled = False
            Frm_Rech_khibra.Show()
        End If
    End Sub

    Private Sub Frm_khibra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.remplirDossier(Comb_marji3ona)

    End Sub

  

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Partager.close()
        Try
            If (txt_num_khibra.Text <> "") Then
                If MsgBox("هل تريد فعلا إضافة هذه الخبرة ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة الخبرات") = MsgBoxResult.Yes Then
                    Dim cli As New Khibra(1, txt_num_khibra.Text, Date_mo9arar.Value.ToString, txt_molahadat.Text)
                    dv.Ajouter_khibra(cli)
                    List_tous.Items.Clear()
                    For Each khib As Khibra In dv.List_khibra
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num.ToString, khib.Date_decision.Date, khib.Remarque}))
                    Next
                    Partager.vider(Me, Partager.nonInitial)
                    MsgBox("تمت إضافة الخبرة بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("idDossNumKhibra") Then
                MsgBox("عفوا،رقم هذه الخبرة استعمل من قبل، المرجو إدخال رقم آخر", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If (List_tous.SelectedIndices.Count <= 0) Then
            MsgBox("عفوا لا يمكنك التعديل، اختر خبرة من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في التعديل")
        Else
            Try

                Partager.close()
                Dim cli As New Khibra(1, txt_num_khibra.Text, Date_mo9arar.Value.ToString, txt_molahadat.Text)
                dv.modifier_khibra(cli, avo.Id)

                List_tous.Items.Clear()
                For Each khib As Khibra In dv.List_khibra
                    List_tous.Items.Add(New ListViewItem(New String() {khib.Num.ToString, khib.Date_decision.Date, khib.Remarque}))
                Next
                MsgBox("تمت عملية التعديل بنجاح", MsgBoxStyle.Information, "نجاح")
                Partager.vider(Me, Partager.nonInitial)
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("idDossNumKhibra") Then
                    MsgBox("عفوا،رقم هذه الخبرة استعمل من قبل، المرجو إدخال رقم آخر", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذه الخبرة ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف المقرر") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Khibra = dv.List_khibra(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_khibra(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Khibra In dv.List_khibra
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num.ToString, khib.Date_decision.Date, khib.Remarque}))
                    Next
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر خبرة من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices

                avo = dv.List_khibra(i)
                txt_num_khibra.Text = avo.Num
                Date_mo9arar.Value = avo.Date_decision
                txt_molahadat.Text = avo.Remarque
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If List_tous.SelectedIndices.Count < 1 Then
            Partager.vider(Me, Partager.nonInitial)
        End If
    End Sub

    Private Sub txt_num_khibra_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_num_khibra.KeyPress
        Dim allowedChars As String = "1234567890/"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_khibra()
                    Exit For
                End If
            Next
            List_tous.Items.Clear()
            For Each khib As Khibra In dv.List_khibra
                List_tous.Items.Add(New ListViewItem(New String() {khib.Num.ToString, khib.Date_decision.Date, khib.Remarque}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

 

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub
End Class